package com.xydzxt.www.controller.student;

import com.xydzxt.www.model.Major;
import com.xydzxt.www.model.Regist;
import com.xydzxt.www.service.MajorService;
import com.xydzxt.www.service.RegistService;
import com.xydzxt.www.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Controller
public class RegistController {
    @Autowired
    RegistService registService;
    @Autowired
    MajorService majorService;



    @GetMapping("/register")
    public String register(HttpServletRequest request,Major major){
        request.setAttribute("majorList",majorService.selectAllMajor(major));
        return "student/signUp";
    }

    @PostMapping("/register")
    public String register(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException{
        req.setCharacterEncoding("utf-8");
        String identityNumber=req.getParameter("identityNumber");
        String password=req.getParameter("password");
        String name=req.getParameter("name");
        String sex=req.getParameter("sex");
        String politicalOutlook=req.getParameter("politicalOutlook");
        String highSchool=req.getParameter("highSchool");
        String email=req.getParameter("email");
        String address=req.getParameter("address");
        String phone=req.getParameter("phone");
        String parentPhone=req.getParameter("parentPhone");
        String headmasterPhone=req.getParameter("headmasterPhone");
        String firstChoice=req.getParameter("firstChoice");
        String secondChoice=req.getParameter("secondChoice");
        String thirdChoice=req.getParameter("thirdChoice");

        if (registService.selectByIdNumber(identityNumber)!=null){
            req.setAttribute("msg","请勿重复注册");
            return "student/msg";
        }else {
            String dateString=new DateUtil().getStringDate("yyMMddHHmmss");
            String examineeNumber=dateString +(new Random().nextInt(900)+100);

            Regist regist=new Regist();
            regist.setExamineeNumber(examineeNumber);
            regist.setIdentityNumber(identityNumber);
            regist.setPassword(password);
            regist.setName(name);
            regist.setSex(Integer.valueOf(sex));
            regist.setPoliticalOutlook(Integer.valueOf(politicalOutlook));
            regist.setHighSchool(highSchool);
            regist.setEmail(email);
            regist.setAddress(address);
            regist.setPhone(phone);
            regist.setParentPhone(parentPhone);
            regist.setHeadmasterPhone(headmasterPhone);
            regist.setFirstChoice(Integer.valueOf(firstChoice));
            regist.setSecondChoice(Integer.valueOf(secondChoice));
            regist.setThirdChoice(Integer.valueOf(thirdChoice));
            regist.setInputName(identityNumber);
            regist.setInputDate(new DateUtil().getStringDate("yyyy-MM-dd HH:mm:ss"));

            boolean isReg=registService.insert(regist);
            if (isReg){
                HttpSession session=req.getSession();
                session.setAttribute("sfzh",identityNumber);
            }
            req.setAttribute("msg","注册成功!");
            return "student/msg";
        }

    }






    }

